package org.mch.func.file.common;

import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JFileChooser;

import org.mch.biz.ui.BizEvent;
import org.mch.biz.ui.BizPanel;

/**
 * 描述：</BR>
 *
 * @author MC、ZL；
 *
 */
public class ClientEvent extends BizEvent {

	private static final long serialVersionUID = 1L;

	JFileChooser fileChooser;

	public ClientEvent(BizPanel panel) {
		super(panel);
	}

	protected void buttonClicked(MouseEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_FileName()) {
			readFileNames();
		}
		if (e.getSource() == getPanel().getBtn_browse()) {
			browse();
		}
	}

	public void browse() {
		int tmp = getFileChooser().showOpenDialog(this.getPanel());
		if (tmp == JFileChooser.APPROVE_OPTION) {
			getPanel().getText_path().setText(getFileChooser().getSelectedFile().getPath());
		}
	}

	public void readFileNames() {
		String prefix = this.getPanel().getText_prefix().getText().trim();

		File file = new File(getPanel().getText_path().getText());
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			StringBuffer sb = new StringBuffer();
			for (File tmpfile : files) {
				if (tmpfile.isDirectory()) {
					if (getPanel().getChckbx_folder().isSelected()) {
						sb.append(prefix);
						sb.append(tmpfile.getName());
						sb.append("\n");
					}
				} else {
					if (getPanel().getChckbx_file().isSelected()) {
						sb.append(prefix);
						sb.append(tmpfile.getName());
						sb.append("\n");
					}
				}
			}
			this.getPanel().getTextArea_up().setText(sb.toString());
		}
	}

	public JFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// ����ѡ��Ŀ¼���ļ�
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}